
function sendAjax(url, method = "GET", data = null) {
    const token = getCookie("token");
    if (token) {
        const headers = new Headers();
        headers.append("Authorization", token);
        headers.set("Content-Type","application/json");
        const request = new Request(url, {
            method: method,
            headers: headers,
            mode: "cors",
            body:JSON.stringify(data)
        });
        return fetch(request).then((response) => {
            if (!response.ok) {
                throw new Error("网络请求失败");
            }
            return response.json();
        }).catch(err=>{
            console.log(err)

        });
    } else {
        alert("请先登录获取 Token");
        window.location = "/login.html"
        console.error("请先登录获取 Token");
    }
}


function sendAjaxNoToken(url, method = "GET", data = null) {
        const headers = new Headers();
        headers.set("Content-Type","application/json");
        const request = new Request(url, {
            method: method,
            headers: headers,
            mode: "cors",
            body:JSON.stringify(data)
        });
        return fetch(request).then((response) => {
            if (!response.ok) {
                throw new Error("网络请求失败");
            }
            return response !== {} ? response.json() : {};
        });
}

